package com.bw.services.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.base.BaseActivity
import com.bw.services.R
import com.bw.services.databinding.ActivityAddParkingBinding
import com.bw.services.entity.AddParkingEntity
import com.bw.services.viewmodel.ServicesViewModel
import com.therouter.router.Route
import dagger.hilt.android.AndroidEntryPoint
import android.widget.PopupMenu
import com.bw.common.utils.LogUtil
import com.bw.services.entity.CarParkingEntity
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.launch
import kotlin.math.log

@AndroidEntryPoint
@Route(path = "app/AddParkingActivity")
class AddParkingActivity : BaseActivity<ActivityAddParkingBinding, ServicesViewModel>() {

    override fun generatePremission(pageName: String): Array<String> {
        TODO("Not yet implemented")
    }

    override fun setupUI() {
        binding.ivBack.setOnClickListener { finish() }

        // 所属区域下拉菜单
        binding.actvArea.setOnClickListener {
            val popup = PopupMenu(this, binding.actvArea)
            popup.menu.add("A园区A栋")
            popup.menu.add("A园区B栋")
            popup.menu.add("A园区C栋")
            popup.setOnMenuItemClickListener { item ->
                binding.actvArea.setText(item.title)
                true
            }
            popup.show()
        }
        // 车位类型下拉菜单
        binding.actvType.setOnClickListener {
            val popup = PopupMenu(this, binding.actvType)
            popup.menu.add("标准车位")
            popup.menu.add("VIP车位")
            popup.menu.add("SVIP车位")
            popup.setOnMenuItemClickListener { item ->
                binding.actvType.setText(item.title)
                true
            }
            popup.show()
        }

        // 车位状态按钮选中逻辑
        val statusButtons = listOf(binding.btnStatusFree, binding.btnStatusOccupied, binding.btnStatusMaintain)
        var selectedStatus = "空闲"
        fun updateStatusButtons(selected: Int) {
            statusButtons.forEachIndexed { idx, btn ->
                btn.isSelected = idx == selected
                btn.setTextColor(if (idx == selected) 0xFF4D6AFF.toInt() else 0xFF222222.toInt())
            }
            selectedStatus = when(selected) {
                0 -> "空闲"
                1 -> "已占用"
                2 -> "维护中"
                else -> "空闲"
            }
        }
        statusButtons.forEachIndexed { idx, btn ->
            btn.setOnClickListener { updateStatusButtons(idx) }
        }
        updateStatusButtons(0) // 默认选中空闲

        // 保存按钮点击
        binding.btnSave.setOnClickListener {
            val entity = CarParkingEntity(
                areaId = 1,
                areaName = binding.actvArea.text.toString(),
                id = 0,
                licensePlate = binding.etPlate.text.toString(),
                remarks = binding.etRemark.text.toString(),
                spaceNumber = binding.etNumber.text.toString(),
                spaceStatus = selectedStatus,
                spaceType = binding.actvType.text.toString(),
                userId = 0,
                userName = binding.etUser.text.toString()
            )
            lifecycleScope.launch {
                vm.getCar_Add_Parking(entity)
            }

        }
    }

    override fun fetchData() {

    }

    override fun observeData() {
        lifecycleScope.launch {
            vm.addParking
                .filterNotNull() // 忽略初始的 null 值
                .collect { result ->
                    LogUtil.d("AddParkingActivity", "添加成功: $result")
                    finish()
                    ToastUtils.showLong("添加成功")
                }
        }
    }

    override fun handleEvent() {

    }

    override val mLayoutResId: Int
        get() = R.layout.activity_add_parking
}